﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ink;

namespace SilverlightInkDemo
{
    public partial class MainPage : UserControl
    {
        Stroke currentStroke;

        List<Stroke> SavedStrokes = new List<Stroke>();

        Stroke replayStroke;
        int strokeIndex;
        int stylusPointIndex;

        public MainPage()
        {
            InitializeComponent();
        }

        private void ReplayTimer_Completed(object sender, EventArgs e)
        {
            if (strokeIndex < SavedStrokes.Count && SavedStrokes[strokeIndex].StylusPoints.Count > stylusPointIndex)
            {
                replayStroke.StylusPoints.Add(SavedStrokes[strokeIndex].StylusPoints[stylusPointIndex]);
                stylusPointIndex++;
                ReplayTimer.Begin();
            }
            else if (SavedStrokes.Count > strokeIndex)
            {
                replayStroke = new Stroke();
                replayStroke.DrawingAttributes.Color = Colors.Yellow;

                inkPresenter.Strokes.Add(replayStroke);

                strokeIndex++;
                stylusPointIndex = 0;

                ReplayTimer.Begin();
            }
        }

        private void inkPresenter_MouseMove(object sender, MouseEventArgs e)
        {
            if (currentStroke != null)
            {
                currentStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter));
            }
        }

        private void inkPresenter_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            currentStroke = null;
            inkPresenter.ReleaseMouseCapture();
        }

        private void inkPresenter_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            inkPresenter.CaptureMouse();
            currentStroke = new Stroke();
            currentStroke.DrawingAttributes.Color = Colors.Red;
            currentStroke.StylusPoints.Add(e.StylusDevice.GetStylusPoints(inkPresenter));
            inkPresenter.Strokes.Add(currentStroke);
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            inkPresenter.Strokes.Clear();
        }

        private void Replay_Click(object sender, RoutedEventArgs e)
        {
            inkPresenter.Strokes.Clear();

            replayStroke = new Stroke();
            replayStroke.DrawingAttributes.Color = Colors.Yellow;

            inkPresenter.Strokes.Add(replayStroke);

            strokeIndex = 0;
            stylusPointIndex = 0;

            ReplayTimer.Begin();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            SavedStrokes.Clear();

            foreach (Stroke s in inkPresenter.Strokes)
            {
                SavedStrokes.Add(s);
            }
        }
    }
}
